package com.frost.blog.service;

import com.frost.blog.mapper.BlogMapper;
import com.frost.blog.mapper.UserMapper;
import com.frost.blog.model.BlogInfo;
import com.frost.blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;

    public UserInfo queryUserByName(String userName) {
        return userMapper.selectByName(userName);
    }

    public UserInfo queryUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    public UserInfo queryAuthorInfoByBlogId(Integer blogId) {
        /**
         * 1、根据博客id获取作者id
         * 2、根据作者id获取作者信息
         */
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if (blogInfo == null || blogInfo.getUserId() < 1) {
            return null;
        }
        return userMapper.selectById(blogInfo.getUserId());
    }
}
